﻿using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using PasswordSafe.Classes;
using PasswordSafe.ListBoxes;
using PasswordSafe.Properties;

namespace PasswordSafe.Templates
{
    public class SelectCategoryTemplate : FluidTemplate
    {
        private FluidLabel titleLabel;
        private FluidButton removeBtn;
        private FluidButton addBtn;

        protected override void InitControl()
        {
            base.InitControl();
            const int w0 = 240;
            const int h0 = 64;
            const int bw1 = 24;
            const int bw2 = 24;
            this.Bounds = new Rectangle(0, 0, w0, h0);
            titleLabel = new FluidLabel("", bw1 + 4, 0, w0 - bw1 - bw2 - 8, h0);
            titleLabel.LineAlignment = StringAlignment.Center;
            titleLabel.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
            Controls.Add(titleLabel);

            Font btnFont = new Font(FontFamily.GenericSansSerif, 6f, FontStyle.Regular);
            removeBtn = new FluidButton("", w0 - bw2 - 4, 4, bw2, 24);
            removeBtn.Image = Resources.minus;
            removeBtn.BackColor = Theme.Current.ItemButtonColor;
            removeBtn.ForeColor = Color.White;
            removeBtn.Font = btnFont;
            removeBtn.Visible = false;
            removeBtn.Click += new EventHandler(removeBtnClick);

            addBtn = new FluidButton("", 4, 4, bw1, 24);
            addBtn.Image = Resources.plus;
            addBtn.BackColor = Theme.Current.ItemButtonColor;
            addBtn.Font = btnFont;
            addBtn.ForeColor = Color.White;
            addBtn.Visible = true;
            addBtn.Click += new EventHandler(addBtnClick);

            Controls.Add(removeBtn);
            Controls.Add(addBtn);

            titleLabel.Anchor = AnchorAll;
            addBtn.Anchor = AnchorTL;
            removeBtn.Anchor = AnchorTR;
        }


        CategoryListBox ListBox { get { return Parent as CategoryListBox; } }

        void addBtnClick(object sender, EventArgs e)
        {
            AddItem();
        }

        void removeBtnClick(object sender, EventArgs e)
        {
            RemoveItem();

        }

        private void AddItem()
        {
            CategoryListBoxItem item = Item as CategoryListBoxItem;
            if (item != null)
            {
                item.IsUsed = true;
                InvalidateItem();
            }
        }

        private void RemoveItem()
        {
            CategoryListBoxItem item = Item as CategoryListBoxItem;
            if (item != null)
            {
                item.IsUsed = false;
                InvalidateItem();
            }
        }

        public void InvalidateItem()
        {
            ListBox.InvalidateItem(ItemIndex);
            //            ListBox.Invalidate();
        }


        protected override void OnItemUpdate(object value)
        {
            base.OnItemUpdate(value);
            CategoryListBoxItem item = value as CategoryListBoxItem;
            if (item != null)
            {
                titleLabel.Text = item.Title;
                removeBtn.Visible = item.IsUsed;
                addBtn.Visible = !item.IsUsed;
                titleLabel.ForeColor = item.IsUsed ? Color.Empty : Theme.Current.GrayTextColor;
            }
            else
            {
                titleLabel.Text = "";
                removeBtn.Visible = false;
                addBtn.Visible = false;
            }
        }
    }
}
